package com.songtech.set.service;


import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.songtech.server.Jedis.JedisBaseDaoImpl;
import com.songtech.server.constant.KeysConstant;
import com.songtech.set.dao.SetDao;
import com.songtech.utils.DateUtil;
import com.songtech.web.entry.Setting;

import javax.annotation.Resource;


/**
 * 
 * @author zhaoxulin
 *description :设置业务层
 */

@Service
public class SetService {

	@Autowired
//	private RedisUtil redisUtil;
	private static String MAKER  = "*HQ";
	
	private final static String SP = ",";
	
	@Autowired
	private  JedisBaseDaoImpl<String>  jedisBaseDaoImpl;
	
	@Resource
	private SetDao  setDao;
	
	/**
	 * 从数据库查询设置表中的信息
	 * @param imei
	 * @return
	 * @throws SQLException
	 */
	public Setting selectSettings(String imei) throws SQLException{
		return setDao.selectSetting(imei);
		
	}

	
	/**
	 * 将设置模式存入缓存(设置和修改）
	 */
	public boolean setPostTimeInterval(Setting bean){
		if(StringUtils.isNotBlank(bean.getImei())){
			//设置智能模式
//			StringBuffer sb1=new StringBuffer(MAKER);
//			sb1.append(SP);
//			sb1.append(bean.getImei());
//			sb1.append(SP);
//			sb1.append("WKMD");
//			sb1.append(SP);
//			sb1.append(DateUtil.format(DateUtil.HOUR_MINUTE_SECOND));
//			sb1.append(SP);
//			sb1.append("2");
//			sb1.append("#");
//			jedisBaseDaoImpl.rpush(KeysConstant.SETTING, sb1.toString());
			
			if(StringUtils.isNotBlank(bean.getPostimeInterval())){
			StringBuffer sb = new StringBuffer(MAKER);
			sb.append(SP);
			sb.append(bean.getImei());
			sb.append(SP);
			sb.append("WKMD");
			sb.append(SP);
			sb.append(DateUtil.format(DateUtil.HOUR_MINUTE_SECOND));
			sb.append(SP);

			sb.append(bean.getPostimeInterval());
			sb.append("#");
			//String  postimeIntervalCmd =  MAKER+","+bean.getImei()+",D1,"+DateUtil.format(DateUtil.HOUR_MINUTE_SECOND)+","+bean.getPostimeInterval()+"#";
			//redisUtil.set("D1_"+bean.getImei(),postimeIntervalCmd);	
			jedisBaseDaoImpl.rpush(KeysConstant.SETTING,sb.toString());
			}
			return true;
		}	
		return false;
	}


	/**
	 * 将短信告警电话缓存(设置和修改）
	 */
	public boolean setMsgPhonenum(Setting bean){
		if(StringUtils.isNotBlank(bean.getImei())){
			if(StringUtils.isNotBlank(bean.getMsgPhonenum())){
			StringBuffer sb = new StringBuffer(MAKER);
			sb.append(SP);
			sb.append(bean.getImei());
			sb.append(SP);
			sb.append("S2");
			sb.append(SP);
			sb.append(DateUtil.format(DateUtil.HOUR_MINUTE_SECOND));
			sb.append(SP);
			sb.append(bean.getMsgPhonenum());
			sb.append("#");	
			//String  msgPhonenumCmd =  MAKER+","+bean.getImei()+",S2,"+DateUtil.format(DateUtil.HOUR_MINUTE_SECOND)+","+bean.getMsgPhonenum()+"#";
			//redisUtil.set("S2_"+bean.getImei(),msgPhonenumCmd);
			jedisBaseDaoImpl.rpush(KeysConstant.SETTING,sb.toString());
			}
			return true;
		}
		return false;
	}
	
	
	/**
	 * 将告警电话写入缓存(设置和修改）
	 */
	public boolean setCallPhonenum(Setting bean){
		if(StringUtils.isNotBlank(bean.getImei())){
			
			if(StringUtils.isNotBlank(bean.getFirstCallPhonenum()) || StringUtils.isNotBlank(bean.getSecondCallPhonenum())){
				
				StringBuffer sb = new StringBuffer(MAKER);
				sb.append(SP);
				sb.append(bean.getImei());
				sb.append(SP);
				sb.append("S3");
				sb.append(SP);
				sb.append(DateUtil.format(DateUtil.HOUR_MINUTE_SECOND));
				sb.append(SP);
				sb.append(bean.getFirstCallPhonenum());
				//当第二个告警电话不为空的时候再拼接上去
				if(StringUtils.isNotEmpty(bean.getSecondCallPhonenum())){
					sb.append(SP);
					sb.append(bean.getSecondCallPhonenum());
				}
				sb.append("#");
				//String  callPhonenumCmd =  MAKER + ","+bean.getImei()+",S3,"+DateUtil.format(DateUtil.HOUR_MINUTE_SECOND)+","+bean.getFirstCallPhonenum()+","+bean.getSecondCallPhonenum()+"#";
				//	redisUtil.set("S3_"+bean.getImei(), callPhonenumCmd);
				jedisBaseDaoImpl.rpush(KeysConstant.SETTING,sb.toString());
				
			}
			return true;
		}
		return false;
	}
	
	
	/**
	 * 设置远程关机
	 * @param set
	 * @return
	 */
	public boolean pwoff(Setting set){
		if(StringUtils.isNotBlank(set.getImei())){
			StringBuffer str = new StringBuffer(MAKER);
			str.append(SP);
			str.append(set.getImei());
			str.append(SP);
			str.append("PWOFF");
			str.append(SP);
			str.append(DateUtil.format(DateUtil.HOUR_MINUTE_SECOND));
			str.append("#");
			jedisBaseDaoImpl.rpush(KeysConstant.SETTING,str.toString());
			return true;
		}
		return false;
	}	

}
